package com.example.poemtest.mine;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.poemtest.R;
import com.example.poemtest.entity.PropInfoBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * created by JHT on 2024/3/31
 * 道具的适配器
 */
public class PropAdapter extends BaseAdapter {

    private Context mContext;
    private List<PropInfoBean.PropInfo> propList;
    private int layoutResId;

    public PropAdapter(Context mContext, List<PropInfoBean.PropInfo> propList, int layoutResId) {
        this.mContext = mContext;
        this.propList = propList;
        this.layoutResId = layoutResId;
    }

    @Override
    public int getCount() {
        if (propList != null) {
            return propList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (propList != null) {
            return propList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            //加载item的布局文件，生成一个View对象，再给converView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.propImg = convertView.findViewById(R.id.prop_img);
            viewHolder.propName = convertView.findViewById(R.id.prop_name);
            viewHolder.propNum = convertView.findViewById(R.id.prop_num);
            viewHolder.imgDuihuan = convertView.findViewById(R.id.prop_duihuan);
            //将ViewHolder对象同converView关联起来
            convertView.setTag(viewHolder);
        } else {
            //重复利用convertView对象
            //获取到跟当前的convertView绑定在啊一起的ViewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //给控件对象绑定要显示的数据
        String str = propList.get(position).getPropPhotoSrc().toString();
//        Uri uri = Uri.parse(str);
//        viewHolder.propImg.setImageURI(uri);
        Picasso.get().load(str).into(viewHolder.propImg);
        viewHolder.propName.setText(propList.get(position).getPropName());
        viewHolder.propNum.setText(propList.get(position).getPropPoint()+"");
        return convertView;
    }

    private static class ViewHolder {
        //每一个item中的控件
        ImageView propImg, imgDuihuan;
        TextView propName, propNum;
    }

}
